package siruna.WE.DeviceDependent.FileManager;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import siruna.WE.DeviceDependent.WidgetEngine1;

public class FileManager {
	WidgetEngine1 we;

	public FileManager(WidgetEngine1 widgetEngine1) {
		// TODO Auto-generated constructor stub
		we = widgetEngine1;
	}

	public HashMap<Integer, ZipFile> getZipFiles() {
		HashMap<Integer, ZipFile> l = new HashMap<Integer, ZipFile>();

		try {
			ZipFile f = new ZipFile(
					"data/data/siruna.WE.DeviceDependent/files/test2.zip");
			l.put(0, f);
			ZipFile f2 = new ZipFile(
					"data/data/siruna.WE.DeviceDependent/files/test.zip");
			l.put(1, f2);
			ZipFile f3 = new ZipFile(
					"data/data/siruna.WE.DeviceDependent/files/widget3.zip");
			l.put(2, f3);

			for (int i = 3; i < 15; i++) {
				// ZipFile f4 = new
				// ZipFile("data/data/siruna.WE/files/test2.zip");
				// l.put(i, f4);
			}

			// unzippen
			Enumeration enumeration = f3.entries();
			System.out.println("unzippen");
			
			
			String base = "data/data/siruna.WE.DeviceDependent/files/"+ f3.getName().substring(f3.getName().lastIndexOf("/"), f3.getName().lastIndexOf(".")) +"/";
			File f1 = new File(base);
			f1.mkdir();
			
			
			while (enumeration.hasMoreElements()) {
				ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
				System.out.println("Unzipping: " + zipEntry.getName());

				BufferedInputStream bis = new BufferedInputStream(f3
						.getInputStream(zipEntry));

				int size;
				byte[] buffer = new byte[2048];

				System.out.println("naam: "+base+zipEntry.getName());
					
			
				FileOutputStream fos = new FileOutputStream(base+zipEntry.getName());
				BufferedOutputStream bos = new BufferedOutputStream(fos,
						buffer.length);

				while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
					bos.write(buffer, 0, size);
				}

				bos.flush();
				bos.close();
				fos.close();

				bis.close();
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return l;
	}

}
